"""
Case Type   : 基础功能
Case Name   : 创建新订阅，订阅端参数synchronous_commit验证
Create At   : 2022/2/15
Owner       : opentestcase026
Description :
    1.在两个集群创建表
    2.创建发布端
    3.订阅端synchronous_commit设置为on
    4.创建订阅synchronous_commit为on
    5.停止订阅端备节点
    6.修改表数据
    7.查询是否同步
    8.恢复订阅端备节点
    9.查询是否同步
    10.订阅端synchronous_commit设置为off
    11.创建订阅synchronous_commit不设置
    12.停止订阅端备节点
    13.修改表数据
    14.查询是否同步
    15.创建订阅synchronous_commit为on
    16.修改表数据
    17.查询是否同步，且事务是否阻塞
    18.订阅端synchronous_commit设置为on
    19.创建订阅synchronous_commit为off
    20.停止订阅端备节点
    21.修改表数据
    22.查询是否同步
Expect      :
    1.成功
    2.成功
    3.成功
    4.成功
    5.成功
    6.成功
    7.阻塞
    8.成功
    9.数据同步
    10.成功
    11.成功
    12.成功
    13.成功
    14.数据同步
    15.成功
    16.成功
    17.订阅阻塞，但事务不阻塞
    18.成功
    19.成功
    20.成功
    21.成功
    22.同步
History     :
    Modified by opentestcase026 2022/4/1:增加most_available_sync参数值判断
    Modified by opentestcase004 2022/8/03:优化用例,增加基础数据同步功能后订阅端有基础
    数据的同时再在订阅端修改表数据，主键冲突，关闭基础数据同步
    Modified by opentestcase004 2022/9/23:优化用例停止订阅备机方式
    Modified by opentestcase012 2022/10/11:优化用例和断言,添加查看数据库状态
"""
import os
import time
import unittest

from testcase.utils.ComThread import ComThread
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro

Primary_SH = CommonSH('PrimaryDbUser')


@unittest.skipIf(3 != Primary_SH.get_node_num(), '非1+2环境不执行')
class Pubsubclass(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info("-----------this is setup-----------")
        self.log.info(f"-----{os.path.basename(__file__)} start-----")
        self.pri_userdb_pub = Node(node='PrimaryDbUser')
        self.pri_userdb_sub = Node(node='remote1_PrimaryDbUser')
        self.constant = Constant()
        self.commsh_pub = CommonSH('PrimaryDbUser')
        self.commsh_sub = CommonSH('remote1_PrimaryDbUser')
        self.commsh_sub_sta = [CommonSH('remote1_Standby1DbUser'),
                               CommonSH('remote1_Standby2DbUser')]
        self.com_pub = Common()
        self.com_sub = Common('remote1_PrimaryDbUser')
        self.subname1 = "sub_case102_1"
        self.pubname1 = "pub_case102_1"
        self.tb_name1 = "t_case102_1"
        self.tb_name2 = "t_case102_2"
        self.hba_pub = os.path.join(macro.DB_INSTANCE_PATH, 'pg_hba.conf')
        self.hba_sub = os.path.join(macro.DB_INSTANCE_PATH_REMOTE1,
                                    'pg_hba.conf')
        self.hba_bak_pub = os.path.join(
            os.path.dirname(macro.DB_INSTANCE_PATH), 'pg_hba.conf')
        self.hba_bak_sub = os.path.join(
            os.path.dirname(macro.DB_INSTANCE_PATH_REMOTE1), 'pg_hba.conf')
        self.port = str(int(self.pri_userdb_pub.db_port) + 1)
        self.wal_level = self.com_pub.show_param("wal_level")
        self.synchronous_commit = self.com_sub.show_param(
            "synchronous_commit", macro.DB_ENV_PATH_REMOTE1)
        self.most_available_sync = self.com_sub.show_param(
            "most_available_sync", macro.DB_ENV_PATH_REMOTE1)
        self.user_param_pub = f'-U {self.pri_userdb_pub.db_user} ' \
            f'-W {self.pri_userdb_pub.db_password}'
        self.user_param_sub = f'-U {self.pri_userdb_sub.db_user} ' \
            f'-W {self.pri_userdb_sub.db_password}'

        text = '-----预置条件1:备份配置文件pg_hba.conf-----'
        self.log.info(text)
        cmd = f'cp {self.hba_pub} {self.hba_bak_pub};'
        self.log.info(cmd)
        msg1 = self.com_pub.get_sh_result(self.pri_userdb_pub, cmd)
        cmd = f'cp {self.hba_sub} {self.hba_bak_sub};'
        self.log.info(cmd)
        msg2 = self.com_pub.get_sh_result(self.pri_userdb_sub, cmd)
        self.assertEqual('', msg1 and msg2, '执行失败:' + text)

        text = '-----预置条件2:查看发布端和订阅端的数据库状态-----'
        self.log.info(text)
        status = self.commsh_pub.get_db_cluster_status()
        self.log.info(status)
        self.assertIn(self.constant.CLUSTER_NORMAL_MSG, status,
                      '执行失败:' + text)
        status = self.commsh_sub.get_db_cluster_status(
            env_path=macro.DB_ENV_PATH_REMOTE1)
        self.log.info(status)
        self.assertIn(self.constant.CLUSTER_NORMAL_MSG, status,
                      '执行失败:' + text)

        text = '-----预置条件3:修改发布端pg_hba文件和配置参数wal_level=logical，重启数据库-----'
        self.log.info(text)
        guc_res = self.commsh_pub.execute_gsguc(
            'reload', self.constant.GSGUC_SUCCESS_MSG, '',
            'all', False, False, '',
            f'host    replication  {self.pri_userdb_pub.db_user} '
            f'{self.pri_userdb_sub.db_host}/32 sha256')
        self.log.info(guc_res)
        self.assertTrue(guc_res, '执行失败:' + text)
        if 'logical' != self.wal_level:
            result = self.commsh_pub.execute_gsguc(
                'set', self.constant.GSGUC_SUCCESS_MSG, 'wal_level=logical')
            self.assertTrue(result, '执行失败:' + text)
            result = self.commsh_pub.restart_db_cluster(False)
            self.log.info(result)
            self.assertTrue(result, '执行失败:' + text)
            status = self.commsh_pub.get_db_cluster_status()
            self.assertIn(self.constant.CLUSTER_NORMAL_MSG, status,
                          '执行失败:' + text)

        text = '-----预置条件4:订阅端修改pg_hba文件,配置参数most_available_sync=off-----'
        self.log.info(text)
        guc_res = self.commsh_sub.execute_gsguc(
            'reload', self.constant.GSGUC_SUCCESS_MSG, '',
            'all', False, False, macro.DB_INSTANCE_PATH_REMOTE1,
            f'host    replication  {self.pri_userdb_pub.db_user} '
            f'{self.pri_userdb_pub.db_host}/32 sha256',
            macro.DB_ENV_PATH_REMOTE1)
        self.log.info(guc_res)
        self.assertTrue(guc_res, '执行失败:' + text)
        if "off" != self.most_available_sync:
            result = self.commsh_sub.execute_gsguc(
                'reload', self.constant.GSGUC_SUCCESS_MSG,
                'most_available_sync=off',
                dn_path=macro.DB_INSTANCE_PATH_REMOTE1,
                env_path=macro.DB_ENV_PATH_REMOTE1)
            self.assertTrue(result, '执行失败:' + text)

    def test_pubsub(self):
        text = '--step1:在两个集群创建表 expect:成功--'
        self.log.info(text)
        create_sql = f'drop table if exists {self.tb_name1};' \
            f'create table {self.tb_name1} (id int primary key,t text);'
        result = self.commsh_pub.execut_db_sql(
            create_sql, sql_type=self.user_param_pub)
        self.log.info(result)
        self.assertIn(self.constant.TABLE_CREATE_SUCCESS,
                      result, '执行失败:' + text)
        result = self.commsh_sub.execut_db_sql(create_sql,
                                               self.user_param_sub, None,
                                               macro.DB_ENV_PATH_REMOTE1)
        self.log.info(result)
        self.assertIn(self.constant.TABLE_CREATE_SUCCESS,
                      result, '执行失败:' + text)

        text = "--step2:创建发布端(集群A) expect:成功--"
        self.log.info(text)
        sql = f"drop publication if exists {self.pubname1};" \
            f"create publication {self.pubname1} for table {self.tb_name1} ;"
        result = self.commsh_pub.execut_db_sql(sql,
                                               sql_type=self.user_param_pub)
        self.log.info(result)
        self.assertIn(self.constant.create_pub_succ_msg, result,
                      '执行失败:' + text)
        self.assertNotIn(self.constant.SQL_WRONG_MSG[1], result,
                         '执行失败:' + text)

        text = "--step3:订阅端synchronous_commit设置为on expect:成功--"
        self.log.info(text)
        result = self.commsh_sub.execute_gsguc(
            'set', self.constant.GSGUC_SUCCESS_MSG,
            'synchronous_commit=on',
            dn_path=macro.DB_INSTANCE_PATH_REMOTE1,
            env_path=macro.DB_ENV_PATH_REMOTE1)
        self.assertTrue(result, '执行失败:' + text)
        result = self.commsh_sub.restart_db_cluster(True,
                                                    macro.DB_ENV_PATH_REMOTE1)
        flg = self.constant.START_SUCCESS_MSG in result or 'Degrade' in result
        self.assertTrue(flg, '执行失败:' + text)

        text = "--step4:创建订阅synchronous_commit为on   expect:创建订阅成功--"
        self.log.info(text)
        result = self.commsh_sub.execute_generate(
            macro.COMMON_PASSWD, env_path=macro.DB_ENV_PATH_REMOTE1)
        self.assertIn('', result, '执行失败:' + text)
        sql = f"drop subscription if exists {self.subname1};" \
            f"create subscription {self.subname1} connection " \
            f"'host={self.pri_userdb_pub.db_host} " \
            f"port={self.port} " \
            f"user={self.pri_userdb_pub.db_user} " \
            f"dbname={self.pri_userdb_pub.db_name} " \
            f"password={self.pri_userdb_pub.db_password}' " \
            f"publication {self.pubname1} with (synchronous_commit=on, " \
            f"copy_data=false);"
        result = self.commsh_sub.execut_db_sql(sql, self.user_param_sub, None,
                                               macro.DB_ENV_PATH_REMOTE1)
        self.log.info(result)
        self.assertNotIn(self.constant.SQL_WRONG_MSG[1],
                         result, '执行失败:' + text)
        self.assertIn(self.constant.create_sub_succ_msg, result,
                      '执行失败:' + text)

        text = "--step5:停止订阅端备节点  expect:成功--"
        self.log.info(text)
        for i in range(len(self.commsh_sub_sta)):
            result = self.commsh_sub_sta[i].stop_db_instance(
                macro.DB_ENV_PATH_REMOTE1, macro.DB_INSTANCE_PATH_REMOTE1)
            self.log.info(result)
            self.assertIn(self.constant.GS_CTL_STOP_SUCCESS_MSG,
                          result, '执行失败:' + text)
        self.log.info('----订阅端状态----')
        status = \
            self.commsh_sub.get_db_cluster_status(
                'detail', env_path=macro.DB_ENV_PATH_REMOTE1)
        self.log.info(status)
        self.assertEqual(2, status.count('Manually stopped'), '执行失败:' + text)
        self.assertIn('Primary Normal', status, '执行失败:' + text)

        text = "--step6:修改表数据  expect:成功--"
        self.log.info(text)
        sql_insert = f"insert into {self.tb_name1} values(1, 'first');"
        result = self.commsh_pub.execut_db_sql(sql_insert,
                                               sql_type=self.user_param_pub)
        self.log.info(result)
        self.assertIn(self.constant.INSERT_SUCCESS_MSG, result, '执行失败' +
                      text)

        text = "--step7:查询是否同步  expect:阻塞--"
        self.log.info(text)
        sql_select = f"select * from {self.tb_name1}"
        select_thread = ComThread(self.commsh_sub.execut_db_sql,
                                  args=(sql_select, self.user_param_sub,
                                        None, macro.DB_ENV_PATH_REMOTE1))
        select_thread.setDaemon(True)
        select_thread.start()
        select_thread.join(10)
        result = select_thread.get_result()
        self.log.info(result)
        self.assertEqual(None, result, '执行失败' + text)

        text = "--step8:恢复订阅端备节点  expect:成功--"
        self.log.info(text)
        for i in range(len(self.commsh_sub_sta)):
            result = \
                self.commsh_sub_sta[i].start_db_instance(
                    'standby', macro.DB_ENV_PATH_REMOTE1,
                    macro.DB_INSTANCE_PATH_REMOTE1)
            self.assertIn(self.constant.REBUILD_SUCCESS_MSG,
                          result, '执行失败:' + text)
        self.log.info('----订阅端状态----')
        status = \
            self.commsh_sub.get_db_cluster_status(
                'detail', env_path=macro.DB_ENV_PATH_REMOTE1)
        self.log.info(status)
        self.assertIn(self.constant.CLUSTER_NORMAL_MSG, status,
                      '执行失败:' + text)

        text = "--step9:查询是否同步  expect:同步--"
        self.log.info(text)
        time.sleep(5)
        result = self.commsh_sub.execut_db_sql(sql_select,
                                               self.user_param_sub,
                                               None,
                                               macro.DB_ENV_PATH_REMOTE1)
        self.log.info(result)
        self.assertIn('1 row', result, '执行失败' + text)
        self.assertIn(' 1 | first', result, '执行失败' + text)

        text = "--step10:订阅端synchronous_commit设置为off expect:成功--"
        self.log.info(text)
        result = self.commsh_sub.execute_gsguc(
            'set', self.constant.GSGUC_SUCCESS_MSG,
            'synchronous_commit=off',
            dn_path=macro.DB_INSTANCE_PATH_REMOTE1,
            env_path=macro.DB_ENV_PATH_REMOTE1)
        self.assertTrue(result, '执行失败:' + text)
        result = self.commsh_sub.restart_db_cluster(True,
                                                    macro.DB_ENV_PATH_REMOTE1)
        flg = self.constant.START_SUCCESS_MSG in result or 'Degrade' in result
        self.assertTrue(flg, '执行失败:' + text)

        text = "--step11:创建订阅synchronous_commit不设置  " \
               "expect:创建订阅成功--"
        self.log.info(text)
        sql = f"drop SUBSCRIPTION if exists {self.subname1};" \
            f"select pg_sleep(2);" \
            f"create subscription {self.subname1} connection " \
            f"'host={self.pri_userdb_pub.db_host} " \
            f"port={self.port} " \
            f"user={self.pri_userdb_pub.db_user} " \
            f"dbname={self.pri_userdb_pub.db_name} " \
            f"password={self.pri_userdb_pub.db_password}' " \
            f"publication {self.pubname1} with (copy_data=false);"
        result = self.commsh_sub.execut_db_sql(sql, self.user_param_sub, None,
                                               macro.DB_ENV_PATH_REMOTE1)
        self.log.info(result)
        self.assertNotIn(self.constant.SQL_WRONG_MSG[1], result,
                         '执行失败:' + text)
        self.assertIn(self.constant.create_sub_succ_msg, result,
                      '执行失败:' + text)
        self.assertIn(self.constant.drop_sub_succ_msg, result,
                      '执行失败:' + text)

        text = "--step12:停止订阅端备节点  expect:成功--"
        self.log.info(text)
        for i in range(len(self.commsh_sub_sta)):
            result = self.commsh_sub_sta[i].stop_db_instance(
                macro.DB_ENV_PATH_REMOTE1, macro.DB_INSTANCE_PATH_REMOTE1)
            self.log.info(result)
            self.assertIn(self.constant.GS_CTL_STOP_SUCCESS_MSG,
                          result, '执行失败:' + text)
        self.log.info('----订阅端状态----')
        status = \
            self.commsh_sub.get_db_cluster_status(
                'detail', env_path=macro.DB_ENV_PATH_REMOTE1)
        self.log.info(status)
        self.assertEqual(2, status.count('Manually stopped'), '执行失败:' + text)
        self.assertIn('Primary Normal', status, '执行失败:' + text)

        text = "--step13:修改表数据  expect:成功--"
        self.log.info(text)
        sql_insert = f"update {self.tb_name1} set id=999;"
        result = self.commsh_pub.execut_db_sql(sql_insert,
                                               sql_type=self.user_param_pub)
        self.log.info(result)
        self.assertIn(self.constant.UPDATE_SUCCESS_MSG, result, '执行失败' +
                      text)

        text = "--step14:查询是否同步  expect:数据同步--"
        self.log.info(text)
        result = self.commsh_sub.execut_db_sql(sql_select,
                                               self.user_param_sub,
                                               None,
                                               macro.DB_ENV_PATH_REMOTE1)
        self.log.info(result)
        self.assertIn('1 row', result, '执行失败' + text)
        self.assertIn('999 | first', result, '执行失败' + text)

        text = "--step15:创建订阅synchronous_commit为on  expect:创建订阅成功--"
        self.log.info(text)
        sql = f"drop subscription if exists {self.subname1};" \
            f"select pg_sleep(2);" \
            f"create subscription {self.subname1} connection " \
            f"'host={self.pri_userdb_pub.db_host} " \
            f"port={self.port} " \
            f"user={self.pri_userdb_pub.db_user} " \
            f"dbname={self.pri_userdb_pub.db_name} " \
            f"password={self.pri_userdb_pub.db_password}' " \
            f"publication {self.pubname1} with (synchronous_commit=on, " \
            f"copy_data=false);"
        result = self.commsh_sub.execut_db_sql(sql, self.user_param_sub, None,
                                               macro.DB_ENV_PATH_REMOTE1)
        self.log.info(result)
        self.assertNotIn(self.constant.SQL_WRONG_MSG[1], result,
                         '执行失败:' + text)
        self.assertIn(self.constant.create_sub_succ_msg, result,
                      '执行失败:' + text)
        self.assertIn(self.constant.drop_sub_succ_msg, result,
                      '执行失败:' + text)

        text = "--step16:修改表数据  expect:成功--"
        self.log.info(text)
        sql_insert = f"insert into {self.tb_name1} values(2, 'second');"
        result = self.commsh_pub.execut_db_sql(sql_insert,
                                               sql_type=self.user_param_pub)
        self.log.info(result)
        self.assertIn(self.constant.INSERT_SUCCESS_MSG, result, '执行失败' +
                      text)

        self.log.info("----等待订阅端数据同步，超时时间为20s----")
        self.commsh_pub.check_sub_consistency(self.subname1,
                                              self.user_param_pub, 20)

        text = "--step17:查询是否同步，且事务是否阻塞  expect:订阅阻塞，" \
               "但事务不阻塞--"
        self.log.info(text)
        self.log.info("-----进行同步表查询-----")
        select_thread = ComThread(self.commsh_sub.execut_db_sql,
                                  args=(sql_select,
                                        self.user_param_sub,
                                        None, macro.DB_ENV_PATH_REMOTE1))
        select_thread.setDaemon(True)
        select_thread.start()
        select_thread.join(10)
        select_result = select_thread.get_result()
        self.log.info(select_result)
        self.log.info("-----进行其他事务操作-----")
        sql = f"drop table if exists {self.tb_name2};" \
            f"create table {self.tb_name2}(i int);" \
            f"select * from {self.tb_name2}"
        other_thread = ComThread(self.commsh_sub.execut_db_sql,
                                 args=(sql,
                                       self.user_param_sub,
                                       None, macro.DB_ENV_PATH_REMOTE1))
        other_thread.setDaemon(True)
        other_thread.start()
        other_thread.join(10)
        other_result = other_thread.get_result()
        self.log.info(other_result)
        if select_result:
            self.log.info("-----如果查询订阅数据结果未阻塞，打印堆栈信息-----")
            gstack_cmd = f"gstack `ps ux|grep primary|grep -v grep|" \
                f"awk {{{{'print $2'}}}}` > ~/gstack_{self.pubname1}.log"
            self.log.info(gstack_cmd)
            self.commsh_sub.node.sh(gstack_cmd).result()
        self.assertEqual(None, select_result, '执行失败' + text)
        self.assertIn(self.constant.CREATE_TABLE_SUCCESS,
                      other_result, '执行失败' + text)
        self.assertIn('0 rows', other_result, '执行失败' + text)

        text = "--step18:订阅端synchronous_commit设置为on expect:成功--"
        self.log.info(text)
        result = self.commsh_sub.execute_gsguc(
            'set', self.constant.GSGUC_SUCCESS_MSG,
            'synchronous_commit=on',
            dn_path=macro.DB_INSTANCE_PATH_REMOTE1,
            env_path=macro.DB_ENV_PATH_REMOTE1)
        self.assertTrue(result, '执行失败:' + text)
        result = self.commsh_sub.stop_db_instance(
            macro.DB_ENV_PATH_REMOTE1, macro.DB_INSTANCE_PATH_REMOTE1,
            '-m immediate')
        self.assertIn(self.constant.GS_CTL_STOP_SUCCESS_MSG,
                      result, '执行失败:' + text)
        result = self.commsh_sub.start_db_cluster(
            True, macro.DB_ENV_PATH_REMOTE1)
        flg = self.constant.START_SUCCESS_MSG in result or 'Degrade' in result
        self.assertTrue(flg, '执行失败:' + text)

        text = "--step19:创建订阅synchronous_commit为off  expect:" \
               "创建订阅成功--"
        self.log.info(text)
        sql = f"drop subscription if exists {self.subname1};" \
            f"select pg_sleep(2);" \
            f"create subscription {self.subname1} connection " \
            f"'host={self.pri_userdb_pub.db_host} " \
            f"port={self.port} " \
            f"user={self.pri_userdb_pub.db_user} " \
            f"dbname={self.pri_userdb_pub.db_name} " \
            f"password={self.pri_userdb_pub.db_password}' " \
            f"publication {self.pubname1} with (synchronous_commit=off, " \
            f"copy_data=false) ;" \
            f"alter subscription {self.subname1} refresh publication;"
        result = self.commsh_sub.execut_db_sql(sql, self.user_param_sub, None,
                                               macro.DB_ENV_PATH_REMOTE1)
        self.log.info(result)
        self.assertNotIn(self.constant.SQL_WRONG_MSG[1], result,
                         '执行失败:' + text)
        self.assertIn(self.constant.create_sub_succ_msg, result,
                      '执行失败:' + text)
        self.assertIn(self.constant.drop_sub_succ_msg, result,
                      '执行失败:' + text)

        text = "--step20:停止订阅端备节点  expect:成功--"
        self.log.info(text)
        time.sleep(10)
        for i in range(len(self.commsh_sub_sta)):
            result = self.commsh_sub_sta[i].stop_db_instance(
                macro.DB_ENV_PATH_REMOTE1, macro.DB_INSTANCE_PATH_REMOTE1)
            self.log.info(result)
            self.assertIn(self.constant.GS_CTL_STOP_SUCCESS_MSG,
                          result, '执行失败:' + text)
        self.log.info('----订阅端状态----')
        status = self.commsh_sub.get_db_cluster_status(
            'detail', env_path=macro.DB_ENV_PATH_REMOTE1)
        self.log.info(status)
        self.assertEqual(2, status.count('Manually stopped'), '执行失败:' + text)
        self.assertIn('Primary Normal', status, '执行失败:' + text)

        text = "--step21:修改表数据  expect:成功--"
        self.log.info(text)
        sql_insert = f"delete from {self.tb_name1};"
        result = self.commsh_pub.execut_db_sql(sql_insert,
                                               sql_type=self.user_param_pub)
        self.log.info(result)
        self.assertIn(self.constant.DELETE_SUCCESS_MSG, result, '执行失败' +
                      text)

        text = "--step22:查询是否同步  expect:同步--"
        self.log.info(text)
        select_thread = ComThread(self.commsh_sub.execut_db_sql,
                                  args=(sql_select,
                                        self.user_param_sub,
                                        None, macro.DB_ENV_PATH_REMOTE1))
        select_thread.setDaemon(True)
        select_thread.start()
        select_thread.join(20)
        result = select_thread.get_result()
        self.log.info(result)
        self.assertIn('0 rows', result, '执行失败:' + text)

    def tearDown(self):
        self.log.info('-----清理环境-----')
        text1 = '-----订阅端数据库启动备机查看集群状态 expect:成功-----'
        self.log.info(text1)
        status1_sub = self.commsh_sub.get_db_cluster_status(
            'status', env_path=macro.DB_ENV_PATH_REMOTE1)
        self.log.info(status1_sub)
        if not status1_sub:
            for i in range(len(self.commsh_sub_sta)):
                self.commsh_sub_sta[i].start_db_instance(
                    'standby', macro.DB_ENV_PATH_REMOTE1,
                    macro.DB_INSTANCE_PATH_REMOTE1)
            status1_sub = self.commsh_sub.get_db_cluster_status(
                'status', env_path=macro.DB_ENV_PATH_REMOTE1)
            self.log.info(status1_sub)

        text2 = '-----删除发布订阅 expect:成功-----'
        self.log.info(text2)
        sql = f"drop subscription if exists {self.subname1};"
        drop_sub_result = self.commsh_sub.execut_db_sql(
            sql, self.user_param_sub, None, macro.DB_ENV_PATH_REMOTE1)
        self.log.info(drop_sub_result)
        sql = f"drop publication if exists {self.pubname1};"
        drop_pub_result = self.commsh_pub.execut_db_sql(
            sql, sql_type=self.user_param_pub)
        self.log.info(drop_pub_result)

        text3 = '-----订阅端和发布端删除表 expect:成功-----'
        self.log.info(text3)
        sql = f"drop table if exists {self.tb_name1};" \
            f"drop table if exists {self.tb_name2};"
        sql_drop_sub = self.commsh_sub.execut_db_sql(sql, self.user_param_sub,
                                                     None,
                                                     macro.DB_ENV_PATH_REMOTE1)
        self.log.info(sql_drop_sub)
        sql_drop_pub = self.commsh_pub.execut_db_sql(
            sql, sql_type=self.user_param_pub)
        self.log.info(sql_drop_pub)

        text4 = '-----订阅端和发布端恢复pg_hba.conf文件 expect:成功-----'
        self.log.info(text4)
        cmd = f'if [[ -e {self.hba_pub} && -e {self.hba_bak_pub} ]]; ' \
            f'then rm -rf {self.hba_pub}; ' \
            f'mv {self.hba_bak_pub} {self.hba_pub}; ' \
            f'else echo not exits; fi'
        hba_msg_pub = self.com_pub.get_sh_result(self.pri_userdb_pub, cmd)
        cmd = f'if [[ -e {self.hba_sub} && -e {self.hba_bak_sub} ]]; ' \
            f'then rm -rf {self.hba_sub}; ' \
            f'mv {self.hba_bak_sub} {self.hba_sub}; ' \
            f'else echo not exits; fi'
        hba_msg_sub = self.com_pub.get_sh_result(self.pri_userdb_sub, cmd)

        text5 = '-----订阅端和发布端恢复参数配置 expect:成功-----'
        self.log.info(text5)
        guc_wal_pub = self.commsh_pub.execute_gsguc(
            'set', self.constant.GSGUC_SUCCESS_MSG,
            f'wal_level={self.wal_level}')
        guc_synccommit_sub = self.commsh_sub.execute_gsguc(
            'set', self.constant.GSGUC_SUCCESS_MSG,
            f'synchronous_commit={self.synchronous_commit}',
            dn_path=macro.DB_INSTANCE_PATH_REMOTE1,
            env_path=macro.DB_ENV_PATH_REMOTE1)
        guc_mostsync_sub = self.commsh_sub.execute_gsguc(
            'reload', self.constant.GSGUC_SUCCESS_MSG,
            f'most_available_sync={self.most_available_sync}',
            dn_path=macro.DB_INSTANCE_PATH_REMOTE1,
            env_path=macro.DB_ENV_PATH_REMOTE1)

        text6 = '-----重启数据库查看数据库状态 expect:成功-----'
        self.log.info(text6)
        res_msg_pub = self.commsh_pub.restart_db_cluster(False)
        status_pub = self.commsh_pub.get_db_cluster_status()
        self.log.info(status_pub)
        res_msg_sub = self.commsh_sub.restart_db_cluster(
            env_path=macro.DB_ENV_PATH_REMOTE1)
        status_sub = self.commsh_sub.get_db_cluster_status(
            env_path=macro.DB_ENV_PATH_REMOTE1)
        self.log.info(status_sub)

        self.assertTrue(status1_sub, '执行失败:' + text1)
        self.assertIn(self.constant.drop_sub_succ_msg, drop_sub_result,
                      '执行失败' + text2)
        self.assertIn(self.constant.drop_pub_succ_msg, drop_pub_result,
                      '执行失败' + text2)
        self.assertEqual(2, sql_drop_sub.count(
            self.constant.TABLE_DROP_SUCCESS), '执行失败' + text3)
        self.assertEqual(2, sql_drop_pub.count(
            self.constant.TABLE_DROP_SUCCESS), '执行失败' + text3)
        self.assertEqual('', hba_msg_pub and hba_msg_sub, '执行失败:' + text4)
        self.assertTrue(guc_wal_pub, '执行失败:' + text5)
        self.assertTrue(guc_synccommit_sub, '执行失败:' + text5)
        self.assertTrue(guc_mostsync_sub, '执行失败:' + text5)
        self.assertTrue(res_msg_pub, '执行失败:' + text6)
        self.assertTrue(res_msg_sub, '执行失败:' + text6)
        self.assertIn(self.constant.CLUSTER_NORMAL_MSG, status_pub,
                      '执行失败:' + text6)
        self.assertIn(self.constant.CLUSTER_NORMAL_MSG, status_sub,
                      '执行失败:' + text6)
        self.log.info(f"-----{os.path.basename(__file__)} end-----")
